/**
 * 用户相关服务
 */
import {
  wechatLogin,
  bindPhone,
  getInfo
} from '../api/login.js'

/**
 * 调用微信登录
 */
function loginByWeixin() {  
  
  wx.removeStorageSync('cookie')
  let code = null;
  return new Promise(function(resolve, reject) {
    return login().then((res) => {
      //登录远程服务器
      
      wx.setStorageSync('needCode',res.code)
      let data = {
        code:res.code
      }
      wechatLogin(data).then(resp => {
        //存储用户信息
        
        console.log('wechatLogin')
        if(resp != 'success'){
          wx.setStorage({key:'newUser',data:resp})
        }else{
          wx.removeStorageSync('newUser');
          wx.setStorage({
            key: 'token',
            data: 'hasLogin',
            success: function(){
              resolve();
            }
          })
        }
       

      }).catch((err) => {
        if (err.data && err.data.code == '010305') {
          setTimeout(function () {
            wx.redirectTo({
              url: '/pages/index/index',
            })
            wx.removeStorageSync('newUser')
          }, 500)
        }
        reject(err);
      });
     
     
    }).catch((err) => {
      
      reject(err);
    })
  });
}

function register(params) {
  return new Promise(function(resolve, reject) {
    //登录远程服务器
    return bindPhone(params).then(res => {
      //存储信息
      wx.setStorage({
        key: 'token',
        data: 'hasLogin',
        success:()=>{
          resolve(res)
        }
      })
    }).catch((err) => {
      reject(err);
    });
  });
}

/**
 * 判断用户是否登录
 */
function checkLogin() {
  return new Promise(function(resolve, reject) {
    
    if (wx.getStorageSync('token')) {
      checkSession().then(() => {
        resolve(true);
      }).catch(() => {
        loginByWeixin().then(() => {
          resolve(true);
        }).catch((err) => {
          reject(err);
        });
      });
    } else {
      loginByWeixin().then(() => {
        resolve(true);
      }).catch((err) => {
        reject(err);
      });
    }
  });
}

/**
 * 获取用户资料
 */
function getUserInfo() {
  return new Promise(function(resolve, reject) {
    getInfo().then(res => {
      resolve(res);
    }).catch((err) => {
      reject(err);
    });
  });
}

/**
 * 调用微信登录
 */
function login() {
  return new Promise(function(resolve, reject) {
    wx.login({
      success: function(res) {
        console.log('res',res)
        if (res.code) {
          //登录远程服务器
          resolve(res);
        } else {
          reject(res);
        }
      },
      fail: function(err) {
        reject(err);
      }
    });
  });
}

/**
 * 检查微信会话是否过期
 */
function checkSession() {
  return new Promise(function (resolve, reject) {
    wx.checkSession({
      success: function () {
        resolve(true);
      },
      fail: function () {
        reject(false);
      }
    })
  });
}

/**
 * 判定是否为新用户
 */
function jundgeNewUser(){
  return new Promise(function(resolve,reject){
    
    if(wx.getStorageSync('newUser')){
      resolve(true)
    }else {
      reject(false)
    }
  })
}

module.exports = {
  loginByWeixin,
  checkLogin,
  register,
  login,
 jundgeNewUser,
  getUserInfo
};